package routes

import (
	"restaurant-system/config"
	"restaurant-system/internal/api/handlers"
	"restaurant-system/internal/middleware"

	"github.com/gin-gonic/gin"
)

func InitRoutes(r *gin.Engine) {
	// 创建 handlers
	userHandler := handlers.NewUserHandler(config.DB)

	// 公开路由
	public := r.Group("/api")
	{
		public.POST("/register", userHandler.Register)
		public.POST("/login", userHandler.Login)

		// 菜品公开接口
		public.GET("/dishes", handlers.GetDishes)
		public.GET("/dishes/:id", handlers.GetDish)

		// 评价公开接口
		public.GET("/reviews/dish/:id", handlers.GetDishReviews)

		// 支付回调（公开路由）
		public.POST("/payments/callback/:method", handlers.PaymentCallback)
	}

	// 需要认证的路由
	authorized := r.Group("/api")
	authorized.Use(middleware.AuthMiddleware())
	{
		// 用户偏好设置
		authorized.PUT("/preferences", handlers.UpdatePreference)
		authorized.GET("/preferences", handlers.GetPreference)
		authorized.POST("/favorites/dishes/:id", handlers.ToggleFavorite)
		authorized.GET("/favorites/dishes", handlers.GetFavorites)
		authorized.POST("/history/dishes/:id", handlers.RecordBrowsingHistory)
		authorized.GET("/history/dishes", handlers.GetBrowsingHistory)
		authorized.GET("/stats/orders", handlers.GetOrderStats)

		// 菜品管理接口
		authorized.POST("/dishes", handlers.CreateDish)
		authorized.PUT("/dishes/:id", handlers.UpdateDish)
		authorized.DELETE("/dishes/:id", handlers.DeleteDish)

		// 订单管理
		authorized.POST("/orders", handlers.CreateOrder)
		authorized.GET("/orders", handlers.GetOrders)
		authorized.GET("/orders/:id", handlers.GetOrder)
		authorized.PUT("/orders/:id/status", handlers.UpdateOrderStatus)

		// 评价系统（需要认证的接口）
		authorized.POST("/reviews", handlers.CreateReview)
		authorized.GET("/reviews/user", handlers.GetUserReviews)
		authorized.PUT("/reviews/:id/reply", handlers.ReplyReview)

		// 积分系统
		authorized.GET("/points", handlers.GetUserPoints)
		authorized.GET("/points/records", handlers.GetPointRecords)
		authorized.POST("/points/use", handlers.UsePoints)

		// 优惠券系统
		authorized.GET("/coupons/templates", handlers.GetCouponTemplates)
		authorized.POST("/coupons/templates", handlers.CreateCouponTemplate)
		authorized.POST("/coupons/exchange", handlers.ExchangeCoupon)
		authorized.GET("/coupons", handlers.GetUserCoupons)
		authorized.POST("/coupons/use", handlers.UseCoupon)

		// 购物车系统
		authorized.POST("/cart", handlers.AddToCart)
		authorized.PUT("/cart/:id", handlers.UpdateCart)
		authorized.DELETE("/cart/:id", handlers.DeleteCartItem)
		authorized.GET("/cart", handlers.GetCart)
		authorized.DELETE("/cart", handlers.ClearCart)

		// 桌位管理
		authorized.POST("/tables", handlers.CreateTable)
		authorized.PUT("/tables/:id", handlers.UpdateTable)
		authorized.DELETE("/tables/:id", handlers.DeleteTable)
		authorized.GET("/tables", handlers.GetTables)

		// 桌位预订
		authorized.POST("/tables/reserve", handlers.ReserveTable)
		authorized.PUT("/reservations/:id/status", handlers.UpdateReservationStatus)
		authorized.GET("/reservations", handlers.GetUserReservations)

		// 支付系统
		authorized.POST("/payments", handlers.CreatePayment)
		authorized.POST("/payments/refund", handlers.CreateRefund)

		// 门店管理
		authorized.POST("/stores", handlers.CreateStore)
		authorized.GET("/stores/nearby", handlers.GetNearbyStores)
		authorized.GET("/stores/:id", handlers.GetStore)

		// 配送地址管理
		authorized.POST("/delivery/addresses", handlers.CreateDeliveryAddress)
		authorized.GET("/delivery/addresses", handlers.GetDeliveryAddresses)
		authorized.PUT("/delivery/addresses/:id/default", handlers.SetDefaultAddress)

		// 文件上传路由
		authorized.POST("/upload", handlers.UploadImage)

		// 用户管理接口
		authorized.GET("/users", userHandler.GetUsers)
		authorized.GET("/users/:id", userHandler.GetUser)
		authorized.PUT("/users/:id", userHandler.UpdateUser)
		authorized.DELETE("/users/:id", userHandler.DeleteUser)

		// 用户消息接口
		messages := authorized.Group("/messages")
		{
			messages.GET("", handlers.GetMessages)
			messages.POST("", handlers.SendMessage)
			messages.GET("/unread", handlers.GetUnreadCount)
		}

		// 用户相关路由组
		userRoutes := authorized.Group("/user")
		{
			userRoutes.GET("/current", userHandler.GetCurrentUser)
		}

		// 菜品相关路由
		dishesGroup := authorized.Group("/dishes")
		{
			dishesGroup.GET("/recommended", handlers.GetRecommendedDishes)
			dishesGroup.GET("/popular", handlers.GetPopularDishes)
		}
	}

	// 添加静态文件服务
	r.Static("/uploads", "./uploads")

	// 管理员路由
	adminGroup := r.Group("/api/admin")
	adminGroup.Use(middleware.AuthMiddleware())
	adminGroup.Use(middleware.AdminMiddleware())
	{
		adminGroup.GET("/orders", handlers.GetAllOrders)
		adminGroup.GET("/orders/:id", handlers.GetOrderByAdmin)
		adminGroup.PUT("/orders/:id/status", handlers.UpdateOrderStatusByAdmin)

		// 评价管理路由
		adminGroup.GET("/reviews", handlers.GetAllReviews)          // 获取所有评价
		adminGroup.POST("/reviews/:id/reply", handlers.ReplyReview) // 回复评价

		// 管理员消息相关接口
		adminGroup.GET("/messages/chats", handlers.GetUserChats)
		adminGroup.GET("/messages/chat/:id", handlers.GetChatMessages)
		adminGroup.POST("/messages/send", handlers.SendAdminMessage)

		// AI客服配置路由
		adminGroup.GET("/ai/config", handlers.GetAIConfig)
		adminGroup.POST("/ai/config", handlers.UpdateAIConfig)

		// 用户订单相关接口
		adminGroup.GET("/users/:userId/orders", handlers.GetUserOrders)
	}
}
